package edu.ucsc.mit2008.swan.editor.coreUI.internal.template.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EditTemplateDialog extends Dialog {

	private Label nameLabel;
	private Text nameText;

	private Label templateLabel;
	private Text templateText;
	private Button templateBrowseButton;

	public EditTemplateDialog(Shell parent) {
		super(parent);
	}

	/**
	 * {@inheritDoc}
	 */
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Edit Template");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(3).applyTo(container);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true,
				true).hint(400, 200).indent(10, 10).applyTo(container);

		Label descLabel = new Label(container, SWT.NONE);
		descLabel.setText("Plase add a template jar and fill the values.");
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true,
				false).span(3, SWT.DEFAULT).applyTo(descLabel);

		nameLabel = new Label(container, SWT.NONE);
		nameLabel.setText("Name");
		GridDataFactory.fillDefaults().applyTo(nameLabel);
		nameText = new Text(container, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true,
				false).span(2, SWT.DEFAULT).applyTo(nameText);

		templateLabel = new Label(container, SWT.NONE);
		templateLabel.setText("Jar file");
		GridDataFactory.fillDefaults().applyTo(templateLabel);
		templateText = new Text(container, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true,
				false).applyTo(templateText);
		templateBrowseButton = new Button(container, SWT.PUSH);
		templateBrowseButton.setText("Browse");
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(false,
				false).applyTo(templateBrowseButton);
		templateBrowseButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
				dialog.setFilterExtensions(new String[] {"*.jar"});
				String file = dialog.open();
				templateText.setText(file == null ? "" : file);
			}

		});
		return container;
	}

	@Override
	protected void okPressed() {
		super.okPressed();
	}

	
}
